package com.hdw.med.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdw.common.util.UUIDUtil;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.BaseIndexService;
import com.hdw.med.bean.model.BaseIndex;
import com.hdw.med.mapper.BaseIndexMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * baseIndex服务实现类
 * Created by hozeData on 2021/06/25.
 */
@Service
@Transactional
public class BaseIndexServiceImpl implements BaseIndexService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseIndexServiceImpl.class);

    @Autowired
    private BaseIndexMapper baseIndexMapper;

    @Override
    public int insert(BaseIndex baseIndex) {
        if(StringUtils.isEmpty(baseIndex.getPk())){
            String uuid32 = UUIDUtil.getUUID32();
            baseIndex.setPk(uuid32);
        }
        return baseIndexMapper.insert(baseIndex);
    }

    @Override
    public int insertSelective(BaseIndex baseIndex)  {
        if(StringUtils.isEmpty(baseIndex.getPk())){
            String uuid32 = UUIDUtil.getUUID32();
            baseIndex.setPk(uuid32);
        }
        return baseIndexMapper.insertSelective(baseIndex);
    }

    @Override
    public int deleteById(String pk)  {
        return baseIndexMapper.deleteByPrimaryKey(pk);
    }

    @Override
    public int updateByIdSelective(BaseIndex baseIndex)  {

        return baseIndexMapper.updateByPrimaryKeySelective(baseIndex);
    }

    @Override
    public int updateById(BaseIndex baseIndex)  {
        return baseIndexMapper.updateByPrimaryKey(baseIndex);
    }

    @Override
    public BaseIndex getById(String pk)  {
        return baseIndexMapper.selectByPrimaryKey(pk);
    }

    @Override
    public List<BaseIndex> findByModel(BaseIndex baseIndex)  {
        return baseIndexMapper.findByModel(baseIndex);
    }

    @Override
    public PaginationVO<BaseIndex> findPageByModel(BaseIndex baseIndex, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseIndex> quesList = baseIndexMapper.findByModel(baseIndex);
        PageInfo<BaseIndex> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseIndex> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public PaginationVO<BaseIndex> findByLike(String filter, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseIndex> quesList = baseIndexMapper.findByLike(filter);
        PageInfo<BaseIndex> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseIndex> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }
}
